source("rglade.R")

g <- new("glade", server="datapath", user="alin");

date <- read(g,date);
customer <- read(g,customer);
supplier <- read(g,supplier);
part <- read(g,part);
lineorder <- read(g,lineorder);

loc <- join(lineorder, lineorder.lo_custkey,
            customer[filter=customer.c_region == 'AMERICA'], customer.c_custkey);
locs <- join(loc, lineorder.lo_suppkey,
             supplier[filter=supplier.s_region == 'AMERICA'], supplier.s_suppkey);
locsp <- join(locs, lineorder.lo_partkey,
              part[filter=(part.p_mfgr == 'MFGR#1' || part.p_mfgr == 'MFGR#2')], part.p_partkey);
locspd <- join(locsp, lineorder.lo_orderdate,
               date, date.d_datekey);

gla_ <- gla(locspd, GLA(GroupByGLA, c(year:INT, nat:VARCHAR), GLA(SumGLA)),
            c(date.d_year, customer.c_nation, 
                  (lineorder.lo_revenue - lineorder.lo_supplycost)),
            c(year:INT, nation:VARCHAR, profit:DOUBLE));

result <- as.data.frame(gla_, year=year, nation=nation, profit=profit);


## gby <- groupby(locspd, c(date.d_year, customer.c_nation),
##                GLA(SumGLA),
##                (lineorder.lo_revenue - lineorder.lo_supplycost),
##                c(year:INT, nation:VARCHAR, profit:DOUBLE));

##result <- as.data.frame(gby, year=year, nation=nation, profit=profit);

show(result);

  
              


